package com.xbongbong.crm.perform.query.detail;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmDetailBaseInfoService;
import com.xbongbong.crm.handle.analysis.CommunicatePlanDetailAnalysisDataHandler;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.OwnerAndCoUserHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.explain.FormDataDetailFormExplainHandleName;
import com.xbongbong.parent.parse.data.FormDataGetAnalysisDataHelp;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CommunicatePlanEnum;
import com.xbongbong.saas.model.CommunicatePlanModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author sunshaoxiang
 * @ClassName CommunicatePlanDetailBaseInfoServiceImpl
 * @Description 访客计划详情页 Base info 业务层
 * @Date 2022/5/24 10:15
 * @Version 1.0
 **/
@Service("communicatePlanDetailBaseInfoServiceImpl")
public class CommunicatePlanDetailBaseInfoServiceImpl extends AbstractCrmDetailBaseInfoService {

    public static final Logger LOGGER = LoggerFactory.getLogger(CommunicatePlanDetailBaseInfoServiceImpl.class);

    @Resource
    private OwnerAndCoUserHelp ownerAndCoUserHelp;
    @Resource
    private FormDataDetailFormExplainHandleName formDataDetailFormExplainHandleName;
    @Resource
    private CommunicatePlanDetailAnalysisDataHandler communicatePlanDetailAnalysisDataHandler;
    @Resource
    private CommunicatePlanModel communicatePlanModel;
    @Resource
    private FormDataGetAnalysisDataHelp formDataGetAnalysisDataHelp;

    @Override
    public List<Integer> businessType() {
        return Collections.singletonList(XbbRefTypeEnum.COMMUNICATE_PLAN.getCode());
    }

    @Override
    public void checkPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer businessType) throws XbbException {

    }

    @Override
    public HandlerExplainVO handleExplain(List<FieldAttrEntity> explainList, PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO) throws XbbException {
        handlerExplainDTO.setExplainList(explainList);
        Integer businessType = handlerExplainDTO.getBusinessType();
        String corpid = handlerExplainDTO.getCorpid();
        String platform = handlerExplainDTO.getPlatform();
        Long formId = formEntity.getId();
        ownerAndCoUserHelp.getOwnerIdAndCoUserId(handlerExplainDTO, handlerExplainDTO.getSaasMark(), handlerExplainDTO.getBusinessType(), paasFormDataEntityExt, new HashMap<>(), DelEnum.NORMAL.getDel());
        HandlerExplainVO handlerExplainVO = formDataDetailFormExplainHandleName.handleExplain(explainList, formEntity, paasFormDataEntityExt, handlerExplainDTO);
        //访客计划解释特殊解析
        handleSpecialExplain(paasFormDataEntityExt.getData(), explainList, businessType, platform, corpid, formId);
        handlerExplainVO.setHeadList(explainList);
        return handlerExplainVO;
    }

    @Override
    public JSONObject analysisData(PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO, HandlerExplainVO handlerExplainVO) throws XbbException {
        List<FieldAttrEntity> list = handlerExplainVO.getHeadList();
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(list.size());
        String corpid = handlerExplainDTO.getCorpid();
        Integer businessType = handlerExplainDTO.getBusinessType();
        Long formId = paasFormDataEntityExt.getFormId();
        list.forEach(item->explainMap.put(item.getAttr(), item));
        JSONObject data = commonAnalysisAllData(paasFormDataEntityExt, handlerExplainDTO, handlerExplainVO, list, explainMap);
        JSONObject specialData =  communicatePlanDetailAnalysisDataHandler.handleDetailSpecialField( paasFormDataEntityExt, businessType, corpid, explainMap, formId, handlerExplainDTO);
        data.putAll(specialData);
        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO();
        BeanUtil.copyProperties(handlerExplainDTO, saasAnalysisDataDTO);
        saasAnalysisDataDTO.setFormId(formId);
        communicatePlanModel.formatData(saasAnalysisDataDTO, data);

        return data;
    }

    /**
     * 访客计划解释特殊解析
     */
    private void handleSpecialExplain(JSONObject data, List<FieldAttrEntity> explainList, Integer businessType, String platForm, String corpid, Long formId) throws XbbException  {
        Iterator<FieldAttrEntity> iterator = explainList.iterator();
        while (iterator.hasNext()) {
            FieldAttrEntity next = iterator.next();
            if (Objects.equals(next.getAttr(), CommunicatePlanEnum.EXECUTOR.getAttr())) {
                next.setFieldType(FieldTypeEnum.TEXT.getType());
            } else if (Objects.equals(next.getAttr(), CommunicatePlanEnum.DELAY_CAUSE.getAttr())) {
                iterator.remove();
            } else if (Objects.equals(next.getAttr(), CommunicatePlanEnum.DELAY_MEMO.getAttr())) {
                iterator.remove();
            }
        }

    }

}
